<?php

namespace app\data\controller;

use app\business\model\IndustryCateModel;
use cmf\controller\AdminBaseController;

class AreaController extends AdminBaseController
{
    function index()
    {
        $industryCateModel = new IndustryCateModel();
        $industry = $industryCateModel
            ->where([
                'parent_id' => ['neq', 0]
            ])
            ->select();

        $this->assign('industry', $industry);
        return $this->fetch();
    }

    /**
     * 获取数据
     */
    function getdata()
    {
        $user = get_access_user();

        $start_time = $this->request->post('start_time', 0, 'strtotime');
        $end_time = $this->request->post('end_time', 0, 'strtotime');
        $industry = $this->request->post('industry', 0, 'intval');
        if (!$end_time) {
            if ($start_time) {
                $end_time = time() + 86399;
            }
        } else {
            $end_time += 86399;
        }

        $result = $this->getarea($user, $start_time, $end_time, $industry);
        $this->result($result, 200);
    }

    /**
     * 获取区域数据
     * @param string $users
     * @param int $start_time
     * @param int $end_time
     * @return array
     */
    function getarea($users = '', $start_time = 0, $end_time = 0, $industry = 0)
    {
        $array = [
            [
                'name' => 'LicensePlate',
                'title' => '卖家'
            ],
//            [
//                'name' => 'Buyer',
//                'title' => '买家'
//            ],
//            [
//                'name' => 'Shoudian',
//                'title' => '售电公司'
//            ],
//            [
//                'name' => 'Medium',
//                'title' => '中介'
//            ],
//            [
//                'name' => 'Follow',
//                'title' => '跟进'
//            ],
//            [
//                'name' => 'VirtualAddress',
//                'title' => '虚拟地址'
//            ],
        ];
        $result = [];
        foreach ($array as $val) {
            if (!auth_check('index', "business/{$val['name']}/")) {
                continue;
            }
            $name = '\\app\\data\\model\\' . $val['name'] . 'Model';
            $model = new $name();
            $user = get_access_user();
            if (!method_exists($model, 'getArea')) {
                continue;
            }
            $data = $model->getArea($user, $start_time, $end_time, $industry);
            $result[$val['name']] = [
                'data' => $data,
                'title' => $val['title']
            ];
        }
        return $result;
    }
}